import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.WindowConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Swatch extends JFrame {
	static boolean continuePing = true;
	private JFrame frame;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		// BufferedImage dest =null;
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Swatch window = new Swatch();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Swatch() {
		initialize();
	}

	public void resetWifi() {
		try {
//			Runtime.getRuntime().exec("cmd /c start arp -d");
//			Runtime.getRuntime().exec("cmd /c start netsh int ip reset");
//			Runtime.getRuntime().exec("cmd /c start ipconfig /flushdns ");
//			Runtime.getRuntime().exec("cmd /c start ipconfig /release 无线网络连接");
//			Runtime.getRuntime().exec("cmd /c start  ipconfig /renew 无线网络连接");
//			Runtime.getRuntime().exec("net start Dhcp");
			Runtime.getRuntime().exec("devcon disable *DEV_8176*");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			Runtime.getRuntime().exec("devcon enable *DEV_8176*");
			try {
				Thread.sleep(4000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	public void beginPing() {
		Runtime runtime = Runtime.getRuntime(); // 获取当前程序的运行进对象
		Process process = null; // 声明处理类对象
		String line = null; // 返回行信息
		InputStream is = null; // 输入流
		InputStreamReader isr = null; // 字节流
		BufferedReader br = null;
		String ip = "202.102.199.68";// 电信dns
		boolean res = false;// 结果
		try {
			process = runtime.exec("ping " + ip); // PING
			is = process.getInputStream(); // 实例化输入流
			isr = new InputStreamReader(is);// 把输入流转换成字节流
			br = new BufferedReader(isr);// 从字节中读取文本
			while ((line = br.readLine()) != null) {
				if (line.contains("TTL")) {
					res = true;
					break;
				}
			}
			is.close();
			isr.close();
			br.close();
			if (res) {
				System.out.println("ping 通  ...");
			} else {
				System.out.println("ping 不通...");
				resetWifi();
			}
		} catch (IOException e) {
			System.out.println(e);
			runtime.exit(1);
		}
	}
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 491, 352);
		final Timer timer = new Timer(15000, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				beginPing();
			}
		});
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
		if (frameSize.height > screenSize.height)
			frameSize.height = screenSize.height;
		if (frameSize.width > screenSize.width)
			frameSize.width = screenSize.width;
		frame.setLocation((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.height) / 2);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		JButton btnNewButton = new JButton("关闭有线");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Runtime.getRuntime().exec("devcon enable *DEV_08B2*");
					Runtime.getRuntime().exec("devcon disable *DEV_153A*");
					Runtime.getRuntime().exec("cmd /c start ping baidu.com ");
					// Runtime.getRuntime()
					// .exec("cmd /c start C:/\"Program Files (x86)\"/\"Google\"/\"Chrome\"/\"Application\"/chrome.exe");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		btnNewButton.setBounds(115, 32, 227, 43);
		frame.getContentPane().add(btnNewButton);

		JButton btnNewButton_1 = new JButton("开启有线");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Runtime.getRuntime().exec("devcon disable *DEV_08B2*");
					Runtime.getRuntime().exec("devcon enable *DEV_153A*");
					try {
						Thread.sleep(2500);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
					Runtime.getRuntime().exec("cmd /c start ping 10.1.7.64 ");
					Runtime.getRuntime().exec(
							"cmd /c start \\\\10.1.7.64\\temp ");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		btnNewButton_1.setBounds(115, 106, 227, 43);
		frame.getContentPane().add(btnNewButton_1);

		JButton btnNewButton_2 = new JButton("退出");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				timer.stop();
				frame.dispose();
			}
		});
		btnNewButton_2.setBounds(115, 237, 227, 43);
		frame.getContentPane().add(btnNewButton_2);

		JButton btnNewButton_3 = new JButton("重启无线");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Runnable run = new Runnable() {
					public void run() {
						//pingDNS();
						timer.start();
					}
				};
				SwingUtilities.invokeLater(run);
			}
		});
		btnNewButton_3.setBounds(115, 177, 227, 50);
		frame.getContentPane().add(btnNewButton_3);
	}
}
